package com.tksflysun.im.api.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tksflysun.im.api.dao.UserAttributeDao;
import com.tksflysun.im.api.dao.UserDao;
import com.tksflysun.im.api.qo.UserQo;
import com.tksflysun.im.api.service.UserService;
import com.tksflysun.im.common.bean.User;
import com.tksflysun.im.common.bean.UserAttribute;
import com.tksflysun.im.common.util.DateUtil;
import com.tksflysun.im.common.util.Md5Util;
import com.tksflysun.im.common.util.StringUtil;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserAttributeDao userAttributeDao;

    @Transactional
    public void addUser(User user) {
        String timeStamp = DateUtil.getTimeStampStr();
        user.setAddTimeStamp(timeStamp);
        user.setUpdateTimeStamp(timeStamp);
        user.setSalt(StringUtil.getRandom(16));
        user.setPassword(Md5Util.getMd5Str(user.getPassword() + user.getSalt()));
        user.setSrlNo(0l);
        user.setReadSrlNo(0l);
        userDao.addUser(user);
        Long userId = user.getUserId();
        UserAttribute userAttribute = user.getAttr();
        userAttribute.setUserId(userId);
        userAttributeDao.addUserAttribute(userAttribute);
    }

    @Override
    public User getUser(UserQo userQo) {
        return userDao.getUser(userQo);
    }

    @Override
    public User getUserWihtPassword(UserQo userQo) {
        return userDao.getUserWihtPassword(userQo);
    }

    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

}
